package com.xinou.daily_report.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinou.common.base.config.ApiResponse;
import com.xinou.daily_report.VO.TaskVO;
import com.xinou.daily_report.entity.*;
import com.xinou.daily_report.enumerate.DynamicEnum;
import com.xinou.daily_report.enumerate.SysConfigEnum;
import com.xinou.daily_report.mapper.SysDynamicMapper;
import com.xinou.daily_report.service.*;
import com.xinou.daily_report.utils.DailyShiroUtil;
import com.xinou.sso.dto.UserRoleDTO;
import com.xinou.sso.mapper.SysUserMapper;
import com.xinou.sso.service.SysUserService;
import com.xinou.sso.util.ShiroUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 王东梁
 * @创建人 wdl
 * @创建时间 2021/7/9
 * @描述
 */
@RestController
@RequestMapping("/mini/task")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(tags = {"前台今日任务"})
public class MiniTaskController {
    private final SysUserService sysUserService;
    private final SysWeekResultService sysWeekResultService;
    private final SysWeekPlanService sysWeekPlanService;
    private final SysWeekTimeService sysWeekTimeService;
    private final SysDailyCommentService sysDailyCommentService;
    private final SysUserMapper sysUserMapper;
    private final SysDailyService sysDailyService;
    private final SysDynamicMapper sysDynamicMapper;
    private final SysFruitService sysFruitService;


    @PostMapping("/dynamic")
    @ApiOperation("本人品牌动态")
    public ApiResponse<DynamicEnum> taskDynamic() {
        Long userId = ShiroUtil.getUserId();
        BigDecimal state0Fruit = sysDynamicMapper.countStateIs0ByUserIdFruit(userId);
        BigDecimal fruitType0Fruit = sysDynamicMapper.countTodayFruitType0ByUserIdFruit(userId);
        BigDecimal addIntegral = sysDynamicMapper.countTodayAddByUserIdIntegral(userId);
        BigDecimal subIntegral = sysDynamicMapper.countTodaySubByUserIdIntegral(userId);
        DynamicEnum.STATE_0_FRUIT.setValue(state0Fruit == null ? BigDecimal.ZERO : state0Fruit);
        DynamicEnum.TODAY_ADD_Fruit.setValue(fruitType0Fruit == null ? BigDecimal.ZERO : fruitType0Fruit);
        DynamicEnum.TODAY_ADD_INTEGRAL.setValue(addIntegral == null ? BigDecimal.ZERO : addIntegral);
        DynamicEnum.TODAY_SUB_INTEGRAL.setValue(subIntegral == null ? BigDecimal.ZERO : subIntegral);
        return new ApiResponse<>(Arrays.asList(DynamicEnum.values()));
    }

    @PostMapping("/list")
    @ApiOperation("获取今日任务列表")
    public ApiResponse<TaskVO> taskList() {
        List<TaskVO> vos = new ArrayList<>();
        Long userId = ShiroUtil.getUserId();
        LocalDate now = LocalDate.now();
        List<String> roleNames = sysUserMapper.userRoleByUserId(userId).getRoleNames();
        boolean isStaff = DailyShiroUtil.isStaff(roleNames);
        boolean isGroupLeader = DailyShiroUtil.isGroupLeader(roleNames);
        boolean isLead = DailyShiroUtil.isLead(roleNames);
        // 日报
        if (isStaff || isGroupLeader) {
            boolean isHave = sysDailyService.count(
                    new LambdaQueryWrapper<SysDaily>()
                            .ne(SysDaily::getState, 2)
                            .eq(SysDaily::getDailyTime, now)
                            .eq(SysDaily::getWorkerId, userId)
            ) < 1;
            if (isHave) {
                vos.add(new TaskVO(1, "24:00前", "填写日报"));
            }
        }
        // 点评次数
        if (isLead) {
            boolean isHave = sysDailyCommentService.count(
                    new LambdaQueryWrapper<SysDailyComment>()
                            .eq(SysDailyComment::getCommentDate, now)
                            .eq(SysDailyComment::getCommentUserId, userId)
            ) < SysConfigEnum.LEAST_COMMENT_TIMES.getValue();
            if (isHave) {
                vos.add(new TaskVO(4, "24:00前", "进行点评"));
            }
        }
        //回复评论
        if (isStaff || isGroupLeader) {
            boolean isHave = sysDailyCommentService.count(
                    new LambdaQueryWrapper<SysDailyComment>()
                            .and(wq -> wq
                                    .eq(SysDailyComment::getCommentDate, now.minusDays(1))
                                    .or()
                                    .eq(SysDailyComment::getCommentDate, now)
                            )
                            .eq(SysDailyComment::getNeedReply, true)
                            .eq(SysDailyComment::getIsReply, false)
                            .eq(SysDailyComment::getDailyWorkerId, userId)
            ) > 0;
            if (isHave) {
                vos.add(new TaskVO(5, "24:00前", "回复评论"));
            }
        }

        //有人回复
        boolean todayHaveReply = sysDailyCommentService.count(
                new LambdaQueryWrapper<SysDailyComment>()
                        .eq(SysDailyComment::getCommentUserId,ShiroUtil.getUserId())
                        .eq(SysDailyComment::getIsReply,true)
                        .eq(SysDailyComment::getReplyDate,now)
        )>0;
        if(todayHaveReply){
            vos.add(new TaskVO(6,"24:00前","有人回复你的消息"));
        }

        //查看催缴信息
        boolean haveUrge = sysFruitService.count(
                new LambdaQueryWrapper<SysFruit>()
                        .eq(SysFruit::getUserId,ShiroUtil.getUserId())
                        .eq(SysFruit::getIsUrge,true)
        )>0;
        if(haveUrge){
            vos.add(new TaskVO(7,"24:00前","水果基金催缴"));
        }

        return new ApiResponse<>(vos);
    }

}
